using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using NotifyCenter.Projects.Dtos;

namespace NotifyCenter.Projects;

[ApiController]
[Route("project")]
public class ProjectController(IProjectService projectService, IEnumerable<ISender> senders) : ControllerBase
{
    [HttpGet]
    public async Task<ActionResult<ProjectPageResponse>> GetProjectsAsync([FromQuery] ProjectPageRequest input, CancellationToken cancellationToken)
    {
        var projects = await projectService.GetListAsync(input, cancellationToken);
        return Ok(projects);
    }

    [HttpPost]
    public async Task<ActionResult<ProjectDto>> CreateProjectAsync([FromBody] ProjectCreateDto input, CancellationToken cancellationToken)
    {
        var project = await projectService.CreateAsync(input, cancellationToken);
        return Ok(project);
    }

    [HttpPut("{projectId:long}")]
    public async Task<ActionResult<ProjectDto>> UpdateProjectAsync(long projectId, [FromBody] ProjectUpdateDto input, CancellationToken cancellationToken)
    {
        var project = await projectService.UpdateAsync(projectId, input, cancellationToken);
        return Ok(project);
    }

    [HttpPut("{projectId:long}/enabled")]
    public async Task<ActionResult<ProjectDto>> SetProjectEnabledAsync(long projectId, [FromQuery] bool enable, CancellationToken cancellationToken)
    {
        var project = await projectService.EnableAsync(projectId, enable, cancellationToken);
        return Ok(project);
    }

    [HttpGet("{projectId:long}/notifications")]
    public async Task<ActionResult<List<NotificationRule>>> GetNotificationsAsync(long projectId, CancellationToken cancellationToken)
    {
        var notifications = await projectService.GetAllNotificationsAsync(projectId, cancellationToken);
        return Ok(notifications);
    }

    [HttpPost("{projectId:long}/notifications")]
    public async Task<ActionResult<List<NotificationRule>>> AddNotificationAsync(long projectId, [FromBody] NotificationRuleRequest notification, CancellationToken cancellationToken)
    {
        var notifications = await projectService.AddNotificationsAsync(projectId, notification, cancellationToken);
        return Ok(notifications);
    }

    [HttpPut("{projectId:long}/notifications/{notificationId:long}")]
    public async Task<ActionResult<List<NotificationRule>>> UpdateNotificationAsync(long projectId, long notificationId, [FromBody] NotificationRuleRequest notification, CancellationToken cancellationToken)
    {
        var notifications = await projectService.UpdateNotificationsAsync(projectId, notificationId, notification, cancellationToken);
        return Ok(notifications);
    }

    [HttpGet("sender-types")]
    public async Task<ActionResult<IEnumerable<string>>> GetSenderTypesAsync()
    {
        return Ok(senders.Select(x => x.Type));
    }
}
